package bpo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.junit.Test;

import entity.Room;
import util.Date_String;
import util.EntityManagerUtil;

public class AdvanceBpo {

	 /*
     * 能够根据客户名或联系电话查询其预订的客房信息，其中，一个客户可以预订多个客房；
     * 能够根据楼层号、房间号查询客房的预订详细信息。
     * */
	public static List<Room> searchAdanvce(String person,String phone){
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = null;
	    sql="SELECT * FROM room r WHERE r.rno in (SELECT a.rno FROM advance a WHERE a.person LIKE '%" + person + "%' AND a.phone LIKE '%" + phone + "%')";
		Query query=em.createNativeQuery(sql, Room.class);
		List<Room> Rooms = query.getResultList();
		em.close();
		return Rooms;
	}
	
	public static List<Map<String,String>> searchList(String person,String phone){
		List<Map<String,String>> tt = new ArrayList<Map<String,String>>();
		List<Room> Rooms = searchAdanvce(person, phone);
		Map<String,String> cc = null;
		for (Room user: Rooms) {
			 cc = new HashMap<String,String>();
			 cc.put("rno", user.getRno());
			 cc.put("fno", user.getFno());
			 cc.put("standard", user.getStandard());
			 cc.put("cost", String.valueOf(user.getCost()));
			 cc.put("position", user.getPosition());
			 cc.put("person",user.getAdvances().get(0).getPerson());
			 cc.put("phone",user.getAdvances().get(0).getId().getPhone());
			 cc.put("btime",Date_String.setStringYmd(user.getAdvances().get(0).getId().getBtime()));
			 cc.put("etime",Date_String.setStringYmd(user.getAdvances().get(0).getEtime()));
			 tt.add(cc);
		}
		return tt;
	}

}
